package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.SystemDictionaryItemQuery;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemServiceImpl;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQuery qo) {
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemServiceImpl.query(qo);
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("result", pageInfo);
        model.addAttribute("systemDictionaries", systemDictionaries);
        return "/systemDictionaryItem/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        systemDictionaryItemServiceImpl.delete(id);
        return new JsonResult("删除成功", true);

    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem, Long id) {
        if (id != null) {
            systemDictionaryItemServiceImpl.update(systemDictionaryItem);
        } else {
            systemDictionaryItemServiceImpl.save(systemDictionaryItem);
        }
        return new JsonResult("新增/编辑成功", true);
    }
}
